package com.lpit.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lpit.pojo.Message;
import com.lpit.utils.Tool;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
* filterName : 过滤器名称
* urlPatterns : 表示要拦截的URL资源，可以是一个或者多个
* */
@WebFilter(filterName = "messageFilter",urlPatterns = {"/*"})
public class MessageFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        /*强转为HttpServletRequest*/
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        System.out.println(request.getRequestURI());

//        /*判断是否为发送消息的请求*/
//        if ((request.getRequestURI().contains("/sendMessage"))) {
//            /*创建工具类*/
//            Tool tool = new Tool();
//            /*将流数据里面数据转换为String*/
//            String string = tool.readValue(request.getReader());
//            /*将jsonString转换为实体对象*/
//            Message message=new ObjectMapper().readValue(string,Message.class);
//            /*排除敏感词，将敏感词修改成*号在设置会实体对象中*/
//            String s = tool.eliminateSensitive(message.getMessageContent());
//            message.setMessageContent(s);
//            /*将对象传入session*/
//            HttpSession session = request.getSession();
//            session.setAttribute("Message",message);
//        }
        /*放行*/
        filterChain.doFilter(request,servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
